package book

import (
	"context"
	"encoding/json"

	"BookStack/pkg/errors"
)

// DomainService 书籍领域服务接口
type DomainService interface {
	// 书籍相关操作
	CreateBook(ctx context.Context, title, slug, author, description, cover, isbn string, categoryID, createdBy uint) (*Book, error)
	GetBookByID(ctx context.Context, id uint) (*Book, error)
	GetBookBySlug(ctx context.Context, slug string) (*Book, error)
	UpdateBook(ctx context.Context, id uint, title, author, description, cover, isbn string, categoryID, updatedBy uint) (*Book, error)
	DeleteBook(ctx context.Context, id uint, deletedBy uint) error
	SetBookStatus(ctx context.Context, id uint, status int, updatedBy uint) (*Book, error)
	SetBookRecommended(ctx context.Context, id uint, recommended int, updatedBy uint) (*Book, error)
	ListBooks(ctx context.Context, offset, limit int, filters map[string]interface{}) ([]*Book, int64, error)
	SearchBooks(ctx context.Context, keyword string, offset, limit int) ([]*Book, int64, error)

	// 章节相关操作
	CreateChapter(ctx context.Context, bookID uint, title, slug, content string, order int, createdBy uint) (*Chapter, error)
	GetChapterByID(ctx context.Context, id uint) (*Chapter, error)
	UpdateChapter(ctx context.Context, id uint, title, content string, updatedBy uint) (*Chapter, error)
	DeleteChapter(ctx context.Context, id uint, deletedBy uint) error
	ListChaptersByBookID(ctx context.Context, bookID uint, offset, limit int) ([]*Chapter, int64, error)
	ReorderChapters(ctx context.Context, bookID uint, orderMap map[uint]int, updatedBy uint) error
	GetPrevChapter(ctx context.Context, bookID uint, currentOrder int) (*Chapter, error)
	GetNextChapter(ctx context.Context, bookID uint, currentOrder int) (*Chapter, error)
	SearchChaptersInBook(ctx context.Context, bookID uint, keyword string, offset, limit int) ([]*Chapter, int64, error)

	// 回收站相关操作
	ListSoftDeletedBooks(ctx context.Context, offset, limit int) ([]*Book, int64, error)
	ListSoftDeletedChapters(ctx context.Context, bookID uint, offset, limit int) ([]*Chapter, int64, error)
	RestoreBook(ctx context.Context, id uint) (*Book, error)
	RestoreChapter(ctx context.Context, id uint) (*Chapter, error)
	HardDeleteBook(ctx context.Context, id uint) error
	HardDeleteChapter(ctx context.Context, id uint) error
	ListRecycleBin(ctx context.Context, offset, limit int) ([]*RecycleItem, int64, error)
	ClearRecycleBin(ctx context.Context) error
}

// bookDomainService 书籍领域服务实现
type bookDomainService struct {
	bookRepo       BookRepository
	chapterRepo    ChapterRepository
	recycleBinRepo RecycleBinRepository
}

// NewDomainService 创建书籍领域服务
func NewDomainService(bookRepo BookRepository, chapterRepo ChapterRepository, recycleBinRepo RecycleBinRepository) DomainService {
	return &bookDomainService{
		bookRepo:       bookRepo,
		chapterRepo:    chapterRepo,
		recycleBinRepo: recycleBinRepo,
	}
}

// CreateBook 创建书籍
func (s *bookDomainService) CreateBook(ctx context.Context, title, slug, author, description, cover, isbn string, categoryID, createdBy uint) (*Book, error) {
	// 检查slug是否已存在
	exists, err := s.bookRepo.CheckSlugExists(ctx, slug, 0)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}
	if exists {
		return nil, errors.New(errors.ErrCodeBookSlugExists, "").WithDetails(map[string]interface{}{
			"slug": slug,
		})
	}

	// 创建书籍实体
	book, err := NewBook(title, slug, author, description, cover, isbn, categoryID, createdBy)
	if err != nil {
		return nil, err
	}

	// 保存书籍
	if err := s.bookRepo.Create(ctx, book); err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	return book, nil
}

// GetBookByID 通过ID获取书籍
func (s *bookDomainService) GetBookByID(ctx context.Context, id uint) (*Book, error) {
	book, err := s.bookRepo.GetByID(ctx, id)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return nil, errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_id": id,
		})
	}

	return book, nil
}

// GetBookBySlug 通过标识获取书籍
func (s *bookDomainService) GetBookBySlug(ctx context.Context, slug string) (*Book, error) {
	book, err := s.bookRepo.GetBySlug(ctx, slug)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return nil, errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_slug": slug,
		})
	}

	return book, nil
}

// UpdateBook 更新书籍
func (s *bookDomainService) UpdateBook(ctx context.Context, id uint, title, author, description, cover, isbn string, categoryID, updatedBy uint) (*Book, error) {
	// 获取书籍
	book, err := s.bookRepo.GetByID(ctx, id)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return nil, errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_id": id,
		})
	}

	// 更新书籍信息
	if err := book.Update(title, author, description, cover, isbn, categoryID, updatedBy); err != nil {
		return nil, err
	}

	// 保存更新
	if err := s.bookRepo.Update(ctx, book); err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	return book, nil
}

// DeleteBook 删除书籍（移入回收站）
func (s *bookDomainService) DeleteBook(ctx context.Context, id uint, deletedBy uint) error {
	// 获取书籍
	book, err := s.bookRepo.GetByID(ctx, id)
	if err != nil {
		return errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_id": id,
		})
	}

	// 软删除书籍
	book.SoftDelete(deletedBy)
	if err := s.bookRepo.Update(ctx, book); err != nil {
		return errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	// 创建回收站记录
	bookData, err := json.Marshal(book)
	if err != nil {
		return errors.Wrap(err, errors.ErrCodeInternal, "JSON 序列化失败")
	}

	recycleItem, err := NewRecycleItem(
		book.ID,
		"book",
		book.Title,
		book.Description,
		string(bookData),
		deletedBy,
	)
	if err != nil {
		return err
	}

	return errors.Wrap(s.recycleBinRepo.Create(ctx, recycleItem), errors.ErrCodeDatabase, "")
}

// SetBookStatus 设置书籍状态
func (s *bookDomainService) SetBookStatus(ctx context.Context, id uint, status int, updatedBy uint) (*Book, error) {
	// 获取书籍
	book, err := s.bookRepo.GetByID(ctx, id)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return nil, errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_id": id,
		})
	}

	// 更新状态
	if err := book.SetStatus(status, updatedBy); err != nil {
		return nil, err
	}

	// 保存更新
	if err := s.bookRepo.Update(ctx, book); err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	return book, nil
}

// SetBookRecommended 设置书籍推荐状态
func (s *bookDomainService) SetBookRecommended(ctx context.Context, id uint, recommended int, updatedBy uint) (*Book, error) {
	// 获取书籍
	book, err := s.bookRepo.GetByID(ctx, id)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return nil, errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_id": id,
		})
	}

	// 更新推荐状态
	if err := book.SetRecommended(recommended, updatedBy); err != nil {
		return nil, err
	}

	// 保存更新
	if err := s.bookRepo.Update(ctx, book); err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	return book, nil
}

// ListBooks 获取书籍列表
func (s *bookDomainService) ListBooks(ctx context.Context, offset, limit int, filters map[string]interface{}) ([]*Book, int64, error) {
	books, total, err := s.bookRepo.List(ctx, offset, limit, filters)
	if err != nil {
		return nil, 0, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}
	return books, total, nil
}

// SearchBooks 搜索书籍
func (s *bookDomainService) SearchBooks(ctx context.Context, keyword string, offset, limit int) ([]*Book, int64, error) {
	books, total, err := s.bookRepo.Search(ctx, keyword, offset, limit)
	if err != nil {
		return nil, 0, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}
	return books, total, nil
}

// CreateChapter 创建章节
func (s *bookDomainService) CreateChapter(ctx context.Context, bookID uint, title, slug, content string, order int, createdBy uint) (*Chapter, error) {
	// 检查书籍是否存在
	book, err := s.bookRepo.GetByID(ctx, bookID)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return nil, errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_id": bookID,
		})
	}

	// 如果没有指定顺序，获取最大顺序号并加1
	if order < 0 {
		maxOrder, err := s.chapterRepo.GetMaxOrderByBookID(ctx, bookID)
		if err != nil {
			return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
		}
		order = maxOrder + 1
	}

	// 创建章节实体
	chapter, err := NewChapter(bookID, title, slug, content, order, createdBy)
	if err != nil {
		return nil, err
	}

	// 保存章节
	if err := s.chapterRepo.Create(ctx, chapter); err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	// 更新书籍章节计数
	book.IncrementChapterCount()
	if err := s.bookRepo.Update(ctx, book); err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	return chapter, nil
}

// GetChapterByID 通过ID获取章节
func (s *bookDomainService) GetChapterByID(ctx context.Context, id uint) (*Chapter, error) {
	chapter, err := s.chapterRepo.GetByID(ctx, id)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if chapter == nil {
		return nil, errors.New(errors.ErrCodeChapterNotFound, "").WithDetails(map[string]interface{}{
			"chapter_id": id,
		})
	}

	return chapter, nil
}

// UpdateChapter 更新章节
func (s *bookDomainService) UpdateChapter(ctx context.Context, id uint, title, content string, updatedBy uint) (*Chapter, error) {
	// 获取章节
	chapter, err := s.chapterRepo.GetByID(ctx, id)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if chapter == nil {
		return nil, errors.New(errors.ErrCodeChapterNotFound, "").WithDetails(map[string]interface{}{
			"chapter_id": id,
		})
	}

	// 更新章节
	if err := chapter.Update(title, content, updatedBy); err != nil {
		return nil, err
	}

	// 保存更新
	if err := s.chapterRepo.Update(ctx, chapter); err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	return chapter, nil
}

// DeleteChapter 删除章节
func (s *bookDomainService) DeleteChapter(ctx context.Context, id uint, deletedBy uint) error {
	// 获取章节
	chapter, err := s.chapterRepo.GetByID(ctx, id)
	if err != nil {
		return errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if chapter == nil {
		return errors.New(errors.ErrCodeChapterNotFound, "").WithDetails(map[string]interface{}{
			"chapter_id": id,
		})
	}

	// 软删除章节
	chapter.SoftDelete(deletedBy)
	if err := s.chapterRepo.Update(ctx, chapter); err != nil {
		return errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	// 获取关联的书籍
	book, err := s.bookRepo.GetByID(ctx, chapter.BookID)
	if err != nil {
		return errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book != nil {
		// 更新书籍章节计数
		book.DecrementChapterCount()
		if err := s.bookRepo.Update(ctx, book); err != nil {
			return errors.Wrap(err, errors.ErrCodeDatabase, "")
		}
	}

	// 创建回收站记录
	chapterData, err := json.Marshal(chapter)
	if err != nil {
		return errors.Wrap(err, errors.ErrCodeInternal, "JSON 序列化失败")
	}

	bookTitle := "未知书籍"
	if book != nil {
		bookTitle = book.Title
	}

	recycleItem, err := NewRecycleItem(
		chapter.ID,
		"chapter",
		chapter.Title,
		"书籍: "+bookTitle,
		string(chapterData),
		deletedBy,
	)
	if err != nil {
		return err
	}

	return errors.Wrap(s.recycleBinRepo.Create(ctx, recycleItem), errors.ErrCodeDatabase, "")
}

// ListChaptersByBookID 获取书籍的章节列表
func (s *bookDomainService) ListChaptersByBookID(ctx context.Context, bookID uint, offset, limit int) ([]*Chapter, int64, error) {
	// 检查书籍是否存在
	book, err := s.bookRepo.GetByID(ctx, bookID)
	if err != nil {
		return nil, 0, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return nil, 0, errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_id": bookID,
		})
	}

	// 获取章节列表
	chapters, total, err := s.chapterRepo.ListByBookID(ctx, bookID, offset, limit)
	if err != nil {
		return nil, 0, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	return chapters, total, nil
}

// ReorderChapters 重新排序章节
func (s *bookDomainService) ReorderChapters(ctx context.Context, bookID uint, orderMap map[uint]int, updatedBy uint) error {
	// 检查书籍是否存在
	book, err := s.bookRepo.GetByID(ctx, bookID)
	if err != nil {
		return errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_id": bookID,
		})
	}

	// 更新章节顺序
	if err := s.chapterRepo.ReorderChapters(ctx, bookID, orderMap); err != nil {
		return errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	return nil
}

// GetPrevChapter 获取上一个章节
func (s *bookDomainService) GetPrevChapter(ctx context.Context, bookID uint, currentOrder int) (*Chapter, error) {
	chapter, err := s.chapterRepo.GetPrevChapter(ctx, bookID, currentOrder)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}
	return chapter, nil
}

// GetNextChapter 获取下一个章节
func (s *bookDomainService) GetNextChapter(ctx context.Context, bookID uint, currentOrder int) (*Chapter, error) {
	chapter, err := s.chapterRepo.GetNextChapter(ctx, bookID, currentOrder)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}
	return chapter, nil
}

// SearchChaptersInBook 搜索书籍中的章节
func (s *bookDomainService) SearchChaptersInBook(ctx context.Context, bookID uint, keyword string, offset, limit int) ([]*Chapter, int64, error) {
	// 检查书籍是否存在
	book, err := s.bookRepo.GetByID(ctx, bookID)
	if err != nil {
		return nil, 0, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return nil, 0, errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_id": bookID,
		})
	}

	// 搜索章节
	chapters, total, err := s.chapterRepo.SearchInBook(ctx, bookID, keyword, offset, limit)
	if err != nil {
		return nil, 0, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	return chapters, total, nil
}

// ListSoftDeletedBooks 获取被删除的书籍列表
func (s *bookDomainService) ListSoftDeletedBooks(ctx context.Context, offset, limit int) ([]*Book, int64, error) {
	books, total, err := s.bookRepo.ListSoftDeleted(ctx, offset, limit)
	if err != nil {
		return nil, 0, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}
	return books, total, nil
}

// ListSoftDeletedChapters 获取被删除的章节列表
func (s *bookDomainService) ListSoftDeletedChapters(ctx context.Context, bookID uint, offset, limit int) ([]*Chapter, int64, error) {
	// 检查书籍是否存在
	book, err := s.bookRepo.GetByID(ctx, bookID)
	if err != nil {
		return nil, 0, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return nil, 0, errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_id": bookID,
		})
	}

	// 获取被删除的章节
	chapters, total, err := s.chapterRepo.ListSoftDeleted(ctx, bookID, offset, limit)
	if err != nil {
		return nil, 0, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	return chapters, total, nil
}

// RestoreBook 恢复被删除的书籍
func (s *bookDomainService) RestoreBook(ctx context.Context, id uint) (*Book, error) {
	// 获取软删除的书籍
	book, err := s.bookRepo.GetSoftDeletedByID(ctx, id)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return nil, errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_id": id,
		})
	}

	// 恢复书籍
	book.Restore()
	if err := s.bookRepo.Update(ctx, book); err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	// 更新回收站记录
	recycleItem, err := s.recycleBinRepo.GetByItemIDAndType(ctx, id, "book")
	if err != nil {
		// 记录不存在的错误可以忽略
		appErr, ok := err.(errors.AppError)
		if !ok || appErr.Code != errors.ErrCodeRecycleBinItemNotFound {
			return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
		}
		// 如果是回收站记录不存在的错误，忽略并继续执行
	} else if recycleItem != nil {
		recycleItem.MarkRestored()
		if err := s.recycleBinRepo.Update(ctx, recycleItem); err != nil {
			return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
		}
	}

	return book, nil
}

// RestoreChapter 恢复被删除的章节
func (s *bookDomainService) RestoreChapter(ctx context.Context, id uint) (*Chapter, error) {
	// 获取软删除的章节
	chapter, err := s.chapterRepo.GetSoftDeletedByID(ctx, id)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if chapter == nil {
		return nil, errors.New(errors.ErrCodeChapterNotFound, "").WithDetails(map[string]interface{}{
			"chapter_id": id,
		})
	}

	// 恢复章节
	chapter.Restore()
	if err := s.chapterRepo.Update(ctx, chapter); err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	// 获取关联的书籍
	book, err := s.bookRepo.GetByID(ctx, chapter.BookID)
	if err != nil {
		return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book != nil {
		// 更新书籍章节计数
		book.IncrementChapterCount()
		if err := s.bookRepo.Update(ctx, book); err != nil {
			return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
		}
	}

	// 更新回收站记录
	recycleItem, err := s.recycleBinRepo.GetByItemIDAndType(ctx, id, "chapter")
	if err != nil {
		// 记录不存在的错误可以忽略
		appErr, ok := err.(errors.AppError)
		if !ok || appErr.Code != errors.ErrCodeRecycleBinItemNotFound {
			return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
		}
		// 如果是回收站记录不存在的错误，忽略并继续执行
	} else if recycleItem != nil {
		recycleItem.MarkRestored()
		if err := s.recycleBinRepo.Update(ctx, recycleItem); err != nil {
			return nil, errors.Wrap(err, errors.ErrCodeDatabase, "")
		}
	}

	return chapter, nil
}

// HardDeleteBook 永久删除书籍
func (s *bookDomainService) HardDeleteBook(ctx context.Context, id uint) error {
	// 获取软删除的书籍
	book, err := s.bookRepo.GetSoftDeletedByID(ctx, id)
	if err != nil {
		return errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if book == nil {
		return errors.New(errors.ErrCodeBookNotFound, "").WithDetails(map[string]interface{}{
			"book_id": id,
		})
	}

	// 永久删除书籍
	if err := s.bookRepo.HardDelete(ctx, id); err != nil {
		return errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	// 删除回收站记录
	recycleItem, err := s.recycleBinRepo.GetByItemIDAndType(ctx, id, "book")
	if err != nil {
		// 记录不存在的错误可以忽略
		appErr, ok := err.(errors.AppError)
		if !ok || appErr.Code != errors.ErrCodeRecycleBinItemNotFound {
			return errors.Wrap(err, errors.ErrCodeDatabase, "")
		}
		// 如果是回收站记录不存在的错误，忽略并继续执行
	} else if recycleItem != nil {
		if err := s.recycleBinRepo.Delete(ctx, recycleItem.ID); err != nil {
			return errors.Wrap(err, errors.ErrCodeDatabase, "")
		}
	}

	return nil
}

// HardDeleteChapter 永久删除章节
func (s *bookDomainService) HardDeleteChapter(ctx context.Context, id uint) error {
	// 获取软删除的章节
	chapter, err := s.chapterRepo.GetSoftDeletedByID(ctx, id)
	if err != nil {
		return errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	if chapter == nil {
		return errors.New(errors.ErrCodeChapterNotFound, "").WithDetails(map[string]interface{}{
			"chapter_id": id,
		})
	}

	// 永久删除章节
	if err := s.chapterRepo.HardDelete(ctx, id); err != nil {
		return errors.Wrap(err, errors.ErrCodeDatabase, "")
	}

	// 删除回收站记录
	recycleItem, err := s.recycleBinRepo.GetByItemIDAndType(ctx, id, "chapter")
	if err != nil {
		// 记录不存在的错误可以忽略
		appErr, ok := err.(errors.AppError)
		if !ok || appErr.Code != errors.ErrCodeRecycleBinItemNotFound {
			return errors.Wrap(err, errors.ErrCodeDatabase, "")
		}
		// 如果是回收站记录不存在的错误，忽略并继续执行
	} else if recycleItem != nil {
		if err := s.recycleBinRepo.Delete(ctx, recycleItem.ID); err != nil {
			return errors.Wrap(err, errors.ErrCodeDatabase, "")
		}
	}

	return nil
}

// ListRecycleBin 获取回收站列表
func (s *bookDomainService) ListRecycleBin(ctx context.Context, offset, limit int) ([]*RecycleItem, int64, error) {
	items, total, err := s.recycleBinRepo.List(ctx, offset, limit)
	if err != nil {
		return nil, 0, errors.Wrap(err, errors.ErrCodeDatabase, "")
	}
	return items, total, nil
}

// ClearRecycleBin 清空回收站
func (s *bookDomainService) ClearRecycleBin(ctx context.Context) error {
	if err := s.recycleBinRepo.Clear(ctx); err != nil {
		return errors.Wrap(err, errors.ErrCodeDatabase, "")
	}
	return nil
}
